package com.sharding4.mybatisplus.seata.controller;

import com.sharding4.mybatisplus.seata.mapper.GoodsMapper;
import com.sharding4.mybatisplus.seata.pojo.BizConstant;
import com.sharding4.mybatisplus.seata.pojo.Goods;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private final GoodsMapper goodsMapper;

    /**
     * 更新商品库存
     *
     * @param goodsId      商品id
     * @param changeAmount 要更新的数量
     */
    // @ShardingTransactionType(TransactionType.BASE)
    @RequestMapping("/stock/{goodsId}/{changeAmount}")
    public String goodsStock(@PathVariable Long goodsId, @PathVariable Integer changeAmount) {
        TransactionTypeHolder.set(TransactionType.BASE);
        int res = goodsMapper.updateGoodsStock(goodsId, changeAmount);
        System.out.println("res:" + res);
        if (res > 0) {
            return BizConstant.SUCCESS;
        } else {
            return BizConstant.FAIL;
        }
    }

    /**
     * 商品详情
     *
     * @param goodsId 商品id
     */
    @GetMapping("/info/{goodsId}")
    public Goods goodsInfo(@PathVariable(value = "goodsId", required = true) Long goodsId) {
        Goods goods = goodsMapper.selectOneGoods(goodsId);
        return goods;
    }
}
